package com.yanyu.init.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yanyu.init.common.ErrorCode;
import com.yanyu.init.exception.BusinessException;
import com.yanyu.init.mapper.FriendLinkMapper;
import com.yanyu.init.model.entity.FriendLink;
import com.yanyu.init.model.vo.FriendVO;
import com.yanyu.init.service.FriendLinkService;
import com.yanyu.init.utils.ThrowUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
* @author 33032
* @description 针对表【link】的数据库操作Service实现
* @createDate 2023-08-25 12:37:30
*/
@Service
public class FriendLinkServiceImpl extends ServiceImpl<FriendLinkMapper, FriendLink> implements FriendLinkService {

    @Override
    public FriendVO getFriendVO(FriendLink friendLink) {
        if (friendLink == null){
            return null;
        }
        FriendVO friendVO =new FriendVO();
        BeanUtils.copyProperties(friendLink,friendVO);
        return friendVO;
    }

    @Override
    public void validFriend(FriendLink friendLink, boolean add) {
        if (friendLink ==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String name = friendLink.getLinkName();
        String profile = friendLink.getLinkProfile();
        if (add) {
            ThrowUtils.throwIf(StringUtils.isAnyBlank(name), ErrorCode.PARAMS_ERROR);
        }
        if (name.length()>=50){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"友链名称过长");
        }
        if (profile.length()>=255){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"友链介绍过长");
        }
    }
}




